package net.neptech.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.client.standard.StandardWebSocketClient;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.messaging.WebSocketStompClient;

/**
 * 这是websocket客户端配置类，用于创建WebSocket客户端实例，并设置心跳和线程池
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig {

    @Value("${websocket.video-service-url}")
    private String videoServiceUrl; // 例如：ws://service-websocket:8080/ws

    @Bean
    public WebSocketStompClient stompClient() {
        StandardWebSocketClient webSocketClient = new StandardWebSocketClient();
        WebSocketStompClient stompClient = new WebSocketStompClient(webSocketClient);

        // 设置心跳
        stompClient.setTaskScheduler(taskScheduler());
        stompClient.setDefaultHeartbeat(new long[]{10000, 10000});

        return stompClient;
    }

    @Bean
    public TaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
        scheduler.setPoolSize(5);
        scheduler.setThreadNamePrefix("StompClient-");
        scheduler.initialize();
        return scheduler;
    }

    @Bean
    @Primary
    public String videoServiceWebSocketUrl() {
        return videoServiceUrl;
    }
}
